home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3.iso / chapter5 / nativsql.c < prev    next >
C/C++ Source or Header  |  1996-04-27  |  7KB  |  237 lines

  1.  
  2. #include <windows.h>  
  3. #include <stdio.h>
  4. #include "nativsql.h"
  5. #include "sqlext.h"  
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "SQLNativeSQL()"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107.  
  108. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  109. {
  110. static HENV  hEnv  = NULL;
  111. static HDBC  hDBC  = NULL;
  112. static HWND  hList = NULL;
  113. UCHAR szSqlStrIn[] = "Select dept_id, { fn LTRIM (dept_name) } From department";
  114.  
  115.    switch( uMsg )
  116.    {
  117.       case WM_CREATE :
  118.               {
  119.                  RETCODE retCode;
  120.  
  121.                  // Allocate Environment and Connection.
  122.                  //.....................................
  123.                  SQLAllocEnv( &hEnv );
  124.                  SQLAllocConnect( hEnv, &hDBC );
  125.  
  126.                  // Connect to the data source
  127.                  //...........................
  128.                  retCode = SQLConnect( hDBC, 
  129.                              "Test Data Src", SQL_NTS,
  130.                              "DBA",           SQL_NTS,  
  131.                              "SQL",           SQL_NTS );
  132.  
  133.                  if ( retCode == SQL_SUCCESS )
  134.                  {
  135.                     int nTabSize = 60;
  136.  
  137.                     hList = CreateWindow( "LISTBOX", "",    
  138.                                LBS_NOTIFY | LBS_USETABSTOPS |
  139.                                LBS_NOINTEGRALHEIGHT  | 
  140.                                WS_BORDER  | WS_CHILD | 
  141.                                WS_VISIBLE | WS_VSCROLL, 
  142.                                0, 0, 0, 0, 
  143.                                hWnd, (HMENU)101,
  144.                                hInst, NULL );
  145.                     
  146.                     SendMessage( hList, LB_SETTABSTOPS, 
  147.                                  1, (LPARAM)&nTabSize );
  148.                  }
  149.               }
  150.               break;
  151.  
  152.       case WM_SIZE :
  153.               MoveWindow( hList, 0, 0, LOWORD( lParam ), 
  154.                                        HIWORD( lParam ), TRUE );
  155.               break; 
  156.               
  157.       case WM_COMMAND :
  158.               switch( LOWORD( wParam ) )
  159.               {
  160.                  case IDM_TEST :
  161.                         {
  162.                            UCHAR   szSqlStr[256];
  163.                            SDWORD  pcbSqlStr;
  164.                            RETCODE retCode;
  165.  
  166.                            // Call SQLNativeSql() to determine 
  167.                            // the statement that is executed 
  168.                            // on the target DBMS.
  169.                            // ................................
  170.                            retCode = SQLNativeSql( hDBC, 
  171.                                         szSqlStrIn, SQL_NTS,
  172.                                         szSqlStr, sizeof(szSqlStr),
  173.                                         &pcbSqlStr );
  174.  
  175.                            if( retCode == SQL_SUCCESS || 
  176.                                retCode == SQL_SUCCESS_WITH_INFO )
  177.                            {
  178.                               SendMessage( hList, LB_ADDSTRING, 
  179.                                            0, (LPARAM)szSqlStr );
  180.                            }
  181.                         }
  182.                         break;
  183.  
  184.                  case IDM_ABOUT :
  185.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  186.                         break;
  187.  
  188.                  case IDM_EXIT :
  189.                         DestroyWindow( hWnd );
  190.                         break;
  191.               }
  192.               break;
  193.       
  194.       case WM_DESTROY :
  195.               PostQuitMessage(0);
  196.  
  197.               // Disconnect Environment.
  198.               //........................
  199.               SQLDisconnect( hDBC );
  200.  
  201.               // Free Connection and Environment.
  202.               //.................................
  203.               SQLFreeConnect( hDBC );
  204.               SQLFreeEnv( hEnv );
  205.               break;
  206.  
  207.       default :
  208.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  209.    }
  210.  
  211.    return( 0L );
  212. }
  213.  
  214.  
  215. LRESULT CALLBACK About( HWND hDlg,           
  216.                         UINT message,        
  217.                         WPARAM wParam,       
  218.                         LPARAM lParam)
  219. {
  220.    switch (message) 
  221.    {
  222.        case WM_INITDIALOG: 
  223.                return (TRUE);
  224.  
  225.        case WM_COMMAND:                              
  226.                if (   LOWORD(wParam) == IDOK         
  227.                    || LOWORD(wParam) == IDCANCEL)    
  228.                {
  229.                        EndDialog(hDlg, TRUE);        
  230.                        return (TRUE);
  231.                }
  232.                break;
  233.    }
  234.  
  235.    return (FALSE); 
  236. }
  237.